package in.muton.panels.paginator.badgehelpers.course;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.auth.roles.DeptRoleCheck;
import in.muton.edushare.auth.roles.UnivRoleCheck;
import in.muton.edushare.wicket.models.CayenneModel;
import in.muton.panels.paginator.helpers.BadgeInterface;

import org.apache.wicket.model.IModel;

import com.edushare.common.dao.CurriculumDAO;
import com.edushare.common.vo.Curricula;
import com.edushare.common.vo.Departments;
import com.edushare.common.vo.Universities;
import com.edushare.common.vo.Users;

public class ProgramBadgeInterface implements BadgeInterface {

	public ProgramBadgeInterface(Universities univ) {
		this.univModel = new CayenneModel<Universities>(univ);
	}

	public ProgramBadgeInterface(Departments dept) {
		this.deptModel = new CayenneModel<Departments>(dept);
	}

	public String removeBadge(IModel<Curricula> programModel) {
		if (programModel.getObject().getBatches().size() > 0) {
			return "Cannot remove the program as there are batches attached to it."
					+ "<br/>Please remove the batches before proceeding.";
		}

		new CurriculumDAO(EduShareSession.get().getSessionORMInterface())
				.deleteCurricula(programModel.getObject());
		return "";
	}

	public boolean isAdmin() {
		Users user = EduShareSession.get().getLoggedInUser();
		if (deptModel != null)
			return DeptRoleCheck.isPrimaryAdmin(deptModel.getObject(), user)||
				DeptRoleCheck.isUnivAdmin(deptModel.getObject(), user);
		if (univModel != null)
			return UnivRoleCheck.isAdmin(univModel.getObject(), user);
		return false;
	}

	private IModel<Universities> univModel;
	private IModel<Departments> deptModel;
	private static final long serialVersionUID = -6645944259217185222L;
}
